package es;

import com.alibaba.fastjson2.JSON;
import es.esentity.Person;
import es.esentity.TestEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Slf4j
public class AddDocs {
    private RestHighLevelClient client;


    @Before
    public void init(){
        HttpHost host = new HttpHost("localhost",9200);
        client = new RestHighLevelClient(RestClient.builder(host));
    }


    @Test
    public void addDoc() throws IOException {
        IndexRequest request = new IndexRequest();
        request.index("test");

//        Person  person = new Person();
//        person.setName("zhangsan");
//        person.setAge(18);
//        person.setAddress("beijing");
//
//        String json =JSON.toJSONString(person);
//        request.source(json, XContentType.JSON);
//        request.id("1000");
//        IndexResponse index = client.index(request, RequestOptions.DEFAULT);
//        log.debug("index:{}",index);

        TestEntity entity = new TestEntity(2,"lisi","you are pig!");
        String json =JSON.toJSONString(entity);
        request.source(json, XContentType.JSON);
        IndexResponse index = client.index(request, RequestOptions.DEFAULT);
        log.debug("index:{}",index);


    }



    //查询文档 利用文档id 查询一个文档详细信息R
    @Test
    public void docDetail() throws IOException {
        //1.查询文档的请求对象
        GetRequest request=new GetRequest();
        //2.设置查询请求的参数
        request.index("index03");//设置索引
        request.id("1000");//文档id
        //3.发起请求 解析响应 拿到所需的数据 包括文档元数据字段  包括文档内容source
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //4.1.解析请求获取所需内容
        System.out.println("当前文档所属索引:"+response.getIndex());
        System.out.println("当前文档的id:"+response.getId());
        //4.2 从响应中解析 写入到文档的内容数据 source
        String sourceAsString = response.getSourceAsString();//文档json格式
        System.out.println(sourceAsString);
        Map<String, Object> sourceAsMap = response.getSourceAsMap();//文档map格式
        //对这个map对象做循环打印
        for (Map.Entry<String, Object> entry : sourceAsMap.entrySet()) {
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
    }
    //根据一个文档对象的id 删除该文档数据D
    @Test
    public void deleteDoc() throws IOException {
        //1.构造一个删除文档的请求
        DeleteRequest request=new DeleteRequest();
        //2.添加请求参数.索引 文档id
        request.index("index02");
        request.id("eMoHEJMBUQeQ_w3uz-YX");
        //3.发送请求 解析响应
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        //解析响应查看响应信息
        response.getResult();//删除结果
        response.getIndex();//删除的索引名称
        response.getId();//删除文档的id
    }

    //修改文档 U
    //修改文档的需求有2种 一种是 数据全覆盖
    //另一种就是部分更新
    @Test
    public void updateDoc() throws IOException {
        //对index02 id=2的文档 做部分更新 更新age的值29-50
        //1.更新请求对象
        UpdateRequest request=new UpdateRequest();
        //2.1设置请求参数 index id 更新的内容有哪些
        request.index("index02");
        request.id("2");
        //2.2设置更新的source 和新增的source格式非常一致
        //使用map设置更新的值 想要更新哪个属性就put哪个
        Map<String,Object> map=new HashMap<>();
        map.put("age",50);
        map.put("gender","女");
        request.doc(map);
        //3.发送请求 解析响应
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        System.out.println(response.getResult());
    }




}
